package com.goalias.service.impl;


import com.goalias.domain.vo.ArchiveCountVo;
import com.goalias.domain.vo.ArchiveVo;
import com.goalias.domain.vo.HotArticleVo;
import com.goalias.mapper.ArchiveMapper;
import com.goalias.resp.PageResult;
import com.goalias.resp.Result;
import com.goalias.service.IArchiveService;
import com.goalias.service.IArticleService;
import com.goalias.utils.IoUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ArchiveServiceImpl implements IArchiveService {
    @Resource
    private ArchiveMapper archiveMapper;

    @Resource
    private IArticleService articleService;

    @Resource
    private IoUtil iOUtil;

    @Override
    public Result getArchiveCountList(Integer pageNum, Integer pageSize) {
        Long total = archiveMapper.selectArchiveTotalCount();
        List<ArchiveCountVo> archiveCountVos = archiveMapper.selectArchiveCount((pageNum - 1) * pageSize, pageSize);
        return Result.success(new PageResult<>(archiveCountVos,(long)archiveCountVos.size(),total));
    }

    @Override
    public Result getArchiveList(Integer pageNum, Integer pageSize) {
        List<ArchiveVo> archiveVos = archiveMapper.selectArchiveList((pageNum - 1) * pageSize, pageSize);
        for (ArchiveVo archiveVo: archiveVos) {
            for ( HotArticleVo hotArticleVo : archiveVo.getArticles()) {
                hotArticleVo.setThumbnail(StringUtils.hasText(hotArticleVo.getThumbnail()) ?
                        iOUtil.getUrl(iOUtil.getBUCKET_NAME(),hotArticleVo.getThumbnail()) :
                        iOUtil.getUrl(iOUtil.getBUCKET_NAME(),iOUtil.DEFAULT_THUMBNAIL()));
            }
        }
        return Result.success(new PageResult<>(archiveVos, (long)archiveVos.size(),articleService.getNormalArticleCount()));
    }
}
